package com.kd.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kd.bean.Book;
import com.kd.service.IBookService;

@Controller
public class BookController {
	
	@Autowired
	private IBookService service;
	
	@RequestMapping("login")
	public String LoginMethod(String bname,String bdesc,
			HttpServletRequest reuqest) {
		try {
			service.LoginBook(bname, bdesc);
			List<Book> listBk = service.findBook(1);
			int count = service.countBk();
			reuqest.setAttribute("listBk", listBk);
			reuqest.setAttribute("limitBk", 1);
			reuqest.setAttribute("count", count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index.jsp";
	}
	
	@RequestMapping("limit")
	public String LimitMethod(Integer limitBk,
			HttpServletRequest reuqest) {
		try {
			int count = service.countBk();
			if(limitBk<=1) {
				limitBk=1;
			}else if(limitBk>=count){
				limitBk=count;
			}
			List<Book> listBk = service.findBook(limitBk);
			reuqest.setAttribute("listBk", listBk);
			reuqest.setAttribute("limitBk", limitBk);
			reuqest.setAttribute("count", count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index.jsp";
	}
	
	@RequestMapping("addBookUrl")
	public String addBookMethod(Book bk,
			HttpServletRequest reuqest) {
		try {
			service.addBook(bk);
			int count = service.countBk();
			List<Book> listBk = service.findBook(1);
			reuqest.setAttribute("listBk", listBk);
			reuqest.setAttribute("limitBk", 1);
			reuqest.setAttribute("count", count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index.jsp";
	}
	@RequestMapping("deleteBookUrl")
	public String deleteBookMethod(HttpServletRequest reuqest,Integer id) {
		try {
			service.deleteBook(id);
			int count = service.countBk();
			List<Book> listBk = service.findBook(1);
			reuqest.setAttribute("listBk", listBk);
			reuqest.setAttribute("limitBk", 1);
			reuqest.setAttribute("count", count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index.jsp";
	}
	
	
	@RequestMapping("findBookByIdUrl")
	public String findBookByIdMethod(HttpServletRequest reuqest,Integer id) {
		try {
			Book bkById = service.findBookById(id);
			reuqest.setAttribute("bkById", bkById);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "update.jsp";
	}
	
	@RequestMapping("updateBookUrl")
	public String updateBookMethod(HttpServletRequest reuqest,Book bk) {
		try {
			service.updateBook(bk);
			int count = service.countBk();
			List<Book> listBk = service.findBook(1);
			reuqest.setAttribute("listBk", listBk);
			reuqest.setAttribute("limitBk", 1);
			reuqest.setAttribute("count", count);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index.jsp";
	}
}	
